<?php
/**
 * Object Relational Persistence Mapping Class for table: lan_kham
 *
 * @package	modules.dept_pharmacy.ordo
 * @author	viethd
 * @version $
 * @copyright 2009 LHV Company
 */
class RhmTreatmentInstance extends ORDataObject {

	/**#@+
	 * Fields of table: lan_kham mapped to class members
	 */
	var $rhm_treatment_instance_id	= '';
	var $ticket_id			= '';
	var $date				= '';
	var $patient_id			= '';
	var $new_patient		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'rhm_treatment_instance';

	/**
	 * Primary Key
	 */
	var $_key = 'rhm_treatment_instance_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'RhmTreatmentInstance';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}

	/**
	 * Check whether an treatment instance is available for today
	 *
	 * @param $ticketId
	 * @return
	 * 		$treatmentInstanceId 	if there is already a treatment instance for today
	 * 		false 					if there is no record for today
	 */
	public function isTreatmentInstanceAvailableForToday($ticketId) {
		$today = date('Y-m-d');
		$sql = "SELECT $this->_key FROM $this->_table WHERE ticket_id = $ticketId and date = '$today'";
		$result = $this->_execute($sql);
		if ($result && !$result->EOF) {
			return $result->fields[$this->_key];
		}
		return false;
	}

    public function isTreatmentInstanceAvailableForDate($ticketId, $date) {
        $today = date('Y-m-d');
        $sql = "SELECT $this->_key FROM $this->_table WHERE ticket_id = $ticketId and date = '$date'";
        $result = $this->_execute($sql);
        if ($result && !$result->EOF) {
            return $result->fields[$this->_key];
        }
        return false;
    }

	/**
	 * Create new treatment instance
	 *
	 * @param $ticketId
	 * @param $patientId
	 * @return treatmentInstanceId
	 */
	public function createTreatmentInstance($ticketId, $patientId) {
		$newPatient = $this->isNewPatient($patientId);
		$data = array (
				'ticket_id'     => $ticketId,
				'patient_id'	=> $patientId,
				'date'			=> date('Y-m-d'),
				'new_patient'	=> $newPatient
			);
		$this->populateArray($data);
		$this->persist();
		return $this->get_id();
	}

	public function getTreatmentList() {
		$today = date('Y-m-d');
		$ds = new Datasource_sql();
		$sql = array (
			'cols'		=> "DISTINCT t.new_patient, p.name, p.person_id,
							pa.patient_code, YEAR(p.date_of_birth) AS birth_year,
							(YEAR(CURDATE()) - YEAR(p.date_of_birth)) AS age,
							p.gender, t.new_patient",
			'from'		=> "$this->_table t
							INNER JOIN person p ON t.patient_id = p.person_id
							INNER JOIN patient pa ON t.patient_id = pa.person_id",
			'where'		=> "t.date = '$today'",
			'orderby'	=> "t.rhm_treatment_instance_id"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}

	/**
	 * Check is there is a first time treatment of this patient
	 * @return 1 if this patient is first time coming here
	 * 		0 old patient
	 */
	private function isNewPatient($patientId) {
		$sql = "SELECT count(rhm_treatment_instance_id) as result FROM $this->_table WHERE patient_id = $patientId";
		$result = $this->_execute($sql);
		if ($result->fields['result'] == 0) {
			return 1;
		}
		return 0;
	}

}
?>
